package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/user")
@Log4j
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("用户分页查询开始");
        String companyId = getLoginCompanyId();
        PageInfo<User> pageInfo = userService.findByPage(companyId, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "system/user/user-list";
    }

    //添加页面
    @RequestMapping("toAdd")
    public String toadd(){
        // 根据企业id查询
        List<User> userList = userService.findAll(getLoginCompanyId());
        request.setAttribute("userList",userList);

        // 查询所有部门，页面下拉列表显示
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",deptList);

        return "system/user/user-add";
    }

    //新建
    @RequestMapping("edit")
    public String edit(User user){
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(user.getId())){
            userService.save(user);
        }else {
            userService.update(user);
        }

        return "redirect:/system/user/list";
    }

    //修改
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        //用户信息
        User user = userService.findById(id);
        //部门下拉列表
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("user",user);
        request.setAttribute("deptList",deptList);
        return "system/user/user-update";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Map<String ,Integer> delete(String id) {
        Map<String, Integer> result = new HashMap<>();
        boolean flag=  userService.delete(id);
        if (flag){
            result.put("message",1);
        }else {
            result.put("message",0);
        }
        return result;
    }

    @RequestMapping("roleList")
    public String roleList(String id){
        //根据id查询
        User user = userService.findById(id);

        //查询所有角色 为复选框准备
        List<Role> roleList = roleService.findAll(getLoginCompanyId());

        request.setAttribute("user",user);
        //3. 查询用户已经拥有的角色（为了页面复选框选中)
        List<Role> userRoleList = roleService.findUserRoleByUserId(id);
        // 保存用户拥有的所有的角色id
        String userRoleIds = "";// "1,2,3," 包含 遍历的角色id ? 选中 : ''
        if (!CollectionUtils.isEmpty(userRoleList)){
        for (Role role : userRoleList) {

            userRoleIds += role.getId()+",";

        }
        }
        request.setAttribute("userRoleIds",userRoleIds);
        request.setAttribute("roleList",roleList);
        request.setAttribute("user",user);
        return "system/user/user-role";
    }

    /**
     * 角色分配权限：
     * 功能入口：角色分配权限：在user-role.jsp 点击保存
     * 请求地址：http://localhost:8080/system/user/changeRole
     * 请求参数：
     *      userId   用户id
     *      roleIds  多个角色id
     * 响应地址：重定向到列表
     */
    @RequestMapping("/changeRole")
    public String changeRole(String userId,String[] roleIds){
        userService.changeRole(userId,roleIds);
        return "redirect:/system/user/list";
    }
}
